home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / src / fig04_17.jar / Ch04 / Fig04_17 / Fig04_17.cpp
C/C++ Source or Header  |  1997-10-13  |  4KB  |  134 lines

  1. // Fig. 4.17: fig04_17.cpp
  2. // This program introduces the topic of survey data analysis.
  3. // It computes the mean, median, and mode of the data.
  4. #include <iostream.h>
  5. #include <iomanip.h>
  6.  
  7. void mean( const int [], int );
  8. void median( int [], int );
  9. void mode( int [], int [], int );
  10. void bubbleSort( int[], int );
  11. void printArray( const int[], int );
  12.  
  13. int main()
  14. {
  15.    const int responseSize = 99;
  16.    int frequency[ 10 ] = { 0 },
  17.        response[ responseSize ] = 
  18.           { 6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
  19.             7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
  20.             6, 7, 8, 9, 3, 9, 8, 7, 8, 7,
  21.             7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
  22.             6, 7, 8, 7, 8, 7, 9, 8, 9, 2,
  23.             7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
  24.             5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
  25.             7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
  26.             7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
  27.             4, 5, 6, 1, 6, 5, 7, 8, 7 };
  28.  
  29.    mean( response, responseSize );
  30.    median( response, responseSize );
  31.    mode( frequency, response, responseSize );
  32.  
  33.    return 0;
  34. }
  35.  
  36. void mean( const int answer[], int arraySize )
  37. {
  38.    int total = 0;
  39.  
  40.    cout << "********\n  Mean\n********\n";
  41.  
  42.    for ( int j = 0; j < arraySize; j++ )
  43.       total += answer[ j ];
  44.  
  45.    cout << "The mean is the average value of the data\n"
  46.         << "items. The mean is equal to the total of\n"
  47.         << "all the data items divided by the number\n"
  48.         << "of data items (" << arraySize 
  49.         << "). The mean value for\nthis run is: " 
  50.         << total << " / " << arraySize << " = "
  51.         << setiosflags( ios::fixed | ios::showpoint )
  52.         << setprecision( 4 ) << ( float ) total / arraySize 
  53.         << "\n\n";
  54. }
  55.  
  56. void median( int answer[], int size )
  57. {
  58.    cout << "\n********\n Median\n********\n"
  59.         << "The unsorted array of responses is";
  60.  
  61.    printArray( answer, size );
  62.    bubbleSort( answer, size );
  63.    cout << "\n\nThe sorted array is";
  64.    printArray( answer, size );
  65.    cout << "\n\nThe median is element " << size / 2
  66.         << " of\nthe sorted " << size 
  67.         << " element array.\nFor this run the median is "
  68.         << answer[ size / 2 ] << "\n\n";
  69. }
  70.  
  71. void mode( int freq[], int answer[], int size )
  72. {
  73.    int rating, largest = 0, modeValue = 0;
  74.  
  75.    cout << "\n********\n  Mode\n********\n";
  76.  
  77.    for ( rating = 1; rating <= 9; rating++ )
  78.       freq[ rating ] = 0;
  79.  
  80.    for ( int j = 0; j < size; j++ )
  81.       ++freq[ answer[ j ] ];
  82.  
  83.    cout << "Response"<< setw( 11 ) << "Frequency"
  84.         << setw( 19 ) << "Histogram\n\n" << setw( 55 )
  85.         << "1    1    2    2\n" << setw( 56 )
  86.         << "5    0    5    0    5\n\n";
  87.  
  88.    for ( rating = 1; rating <= 9; rating++ ) {
  89.       cout << setw( 8 ) << rating << setw( 11 )
  90.            << freq[ rating ] << "          ";
  91.  
  92.       if ( freq[ rating ] > largest ) {
  93.          largest = freq[ rating ];
  94.          modeValue = rating;
  95.       }
  96.  
  97.       for ( int h = 1; h <= freq[ rating ]; h++ )
  98.          cout << '*';
  99.  
  100.       cout << '\n';
  101.    }
  102.  
  103.    cout << "The mode is the most frequent value.\n"
  104.         << "For this run the mode is " << modeValue
  105.         << " which occurred " << largest << " times." << endl;
  106. }
  107.  
  108. void bubbleSort( int a[], int size )
  109. {
  110.    int hold;
  111.  
  112.    for ( int pass = 1; pass < size; pass++ )
  113.  
  114.       for ( int j = 0; j < size - 1; j++ )
  115.  
  116.          if ( a[ j ] > a[ j + 1 ] ) {
  117.             hold = a[ j ];
  118.             a[ j ] = a[ j + 1 ];
  119.             a[ j + 1 ] = hold;
  120.          }
  121. }
  122.  
  123. void printArray( const int a[], int size )
  124. {
  125.    for ( int j = 0; j < size; j++ ) {
  126.  
  127.       if ( j % 20 == 0 )
  128.          cout << endl;
  129.  
  130.       cout << setw( 2 ) << a[ j ];
  131.    }
  132. }
  133.  
  134.